﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IISLogAnalyzer.Common
{
    public class Logger
    {
        private static object logLockObj = new object();
        private static string LogFilePath;

        public static void WriteLog(string logTitle, string message)
        {
            lock (logLockObj)
            {
                // 获取应用程序的根目录，并构建日志文件的完整路径  
                string appRootPath = AppDomain.CurrentDomain.BaseDirectory;
                LogFilePath = Path.Combine(appRootPath + "/logs/", logTitle + "_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm") + ".log");
                // 确保日志目录存在  
                string logDirectory = Path.GetDirectoryName(LogFilePath);
                if (!Directory.Exists(logDirectory))
                {
                    Directory.CreateDirectory(logDirectory);
                }

                // 写入日志消息到文件  
                using (StreamWriter writer = File.AppendText(LogFilePath))
                {
                    writer.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - " + message);
                    writer.Flush();
                }
            }
        }
    }
}
